import { wxRequest } from "../../utils/ajax";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    list : [],
    current: 0,
    pageStart: 1,
    pageTotal: 5,
    paging:true,
  },
  onLoad: function () {
    this.getArticles(this.data.current, this.data.pageStart, this.data.pageTotal);
  },
  //tab
  getCurrentTab: function (e) {
    // console.log(e);
    let that = this, cur = e.detail;
    that.setData({
      current: cur,
    });
    that.getArticles(cur, 1, that.data.pageTotal);
  },
  getArticles: function (cur, pageStart, pageTotal) {
    let that = this;
    wxRequest('/aArticle/list', {
      type: cur, pageStart: pageStart, pageTotal: pageTotal
    }, function (data) {
      let arr = that.data.list;
      let res = data.data.data;
      var tempList = null;
      if (cur == 1) {
        tempList = res;
      } else {
        tempList = res;
      }
      if (pageStart == 1) {
        arr = [];
        that.setData({
          pageStart: pageStart
        });
      }
      if (res == null) {
        that.setData({
          paging: false,
          list: null,
          pageStart: 1
        });
      } else {
        if (res.length < pageTotal) {
          that.setData({
            paging: false,
            list: arr.concat(tempList),
          });
        } else {
          that.setData({
            paging: true,
            pageStart: pageStart + 1,
            list: arr.concat(tempList),
          });
        }
      }
    });
  },
  bindArticleDetail : function(e){
    let artId = e.currentTarget.dataset.id;
    console.log(artId);
    if(artId!=""&&artId!=undefined){
      wx.navigateTo({
        url: '/pages/detail/detail?artId=' + artId
      })
    }
  },
  onReachBottom: function () {
    let that = this;
    if (that.data.paging) {
      that.getArticles(that.data.current, that.data.pageStart, that.data.pageTotal);
    }
  }
})